package com.llyb.lease.web.admin.custom.convert;


import com.llyb.lease.model.enums.BaseEnum;
import com.llyb.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.URLEncoder;

/**
 * projectName: com.atguigu.lease.web.admin.custom.converter
 *
 * @author: llyb
 * description: string -> anyEnumType
 */
@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {


    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String source) {
                for (T enumConstant : targetType.getEnumConstants()) {
                    if (enumConstant.getCode().equals(Integer.valueOf(source))) {
                        return enumConstant;
                    }
                }
                throw new RuntimeException("非法的参数异常，只接受0，1"+source);
            }
        };
    }
}